﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Configuration;

namespace DocumentManager
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 form = new Form1();
            AddDocument addDoc = new AddDocument();
            addDoc.MdiParent = form;
            addDoc.WindowState = FormWindowState.Maximized;
            addDoc.Show();
            ApplicationEventHandlerClass AppEvents = new ApplicationEventHandlerClass();
            Application.ThreadExit += new EventHandler(AppEvents.OnThreadExit);
            Application.Run(form);
            
        }

        public class ApplicationEventHandlerClass
        {

            public void OnThreadExit(object sender, EventArgs e)
            {
                //delete all temp files created
                String path = ConfigurationManager.AppSettings["TargetTempDir"].ToString();
                try
                {
                    if (System.IO.Directory.Exists(path))
                    {
                        string[] files = System.IO.Directory.GetFiles(path);
                        foreach (string file in files)
                        {
                            Console.WriteLine("File extension." + file.Substring(file.LastIndexOf(".")).ToUpper());
                            if ( file.Substring(file.LastIndexOf(".")).ToUpper().Equals(".PDF"))
                            {
                                try
                                {
                                    System.IO.File.Delete(file);
                                }
                                catch { }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string err = ex.Message;
                }
            }
        }
    }
}
